<template>
  <div>
    <!-- data：表格的数据 -->
    <!-- default-expand-all：默认展开所有子节点 -->
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
    >
      <!-- prop：要显示的字段 -->
      <!-- sortable：排序 -->
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="rolename" label="角色名称"></el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click="edit(scope.row)"
            size="mini"
            type="success"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            @click="del(scope.row.id)"
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {getRoleList} from '../../request'
export default {
  data() {
    return {
      tableData: [],
    };
  },
  created() {
    // 获取表格中的数据
    this.getTableData();
  },
  methods: {
    // 获取表格中的数据
    async getTableData() {
      this.tableData = await getRoleList();
    },
    // 编辑角色
    edit(data) {
      // 组件传值
      this.$emit('changeInfo',data);
    },
    // 删除菜单
    del(id) {
      this.$confirm("确定删除吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 点击确认按钮，真的删除
          this.delMenu(id);
        })
        .catch(() => {
          // 点击取消按钮，不删除
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 真的删除
    delMenu(id) {
      this.$http
        .post("/menudelete", { id: id })
        .then((res) => {
          if(res.code === 200) {
            this.tableData = res.list;
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style>
</style>